package nus.iss.laps;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpSession;

import nus.iss.laps.cri.LeaveCri;
import nus.iss.laps.dao.LeaveDAO;
import nus.iss.laps.general.LapsUtil;
import nus.iss.laps.model.Leave;
import nus.iss.laps.model.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.WebRequest;

@Controller
public class ReportController {

	@Autowired
	LeaveDAO leaveDAO;
	
	@RequestMapping(value="/reports/leave", method = RequestMethod.GET)
	public String prepareLeaveRpt(Model model)
	{
		return "prepare_leave_rpt";
	}
	
	@RequestMapping(value="/reports/leave", method = RequestMethod.POST)
	public String showLeaveRpt(Model model, @ModelAttribute("SpringWeb") LeaveCri cri, HttpSession session)
	{
		try {
			cri.setEmployeID(((User)session.getAttribute("user")).getLoginID());
			cri.setStartDate(LapsUtil.formattedDateToString(cri.getStartDate()));
			cri.setEndDate(LapsUtil.formattedDateToString(cri.getEndDate()));
			List<Leave> l_LeaveList = leaveDAO.prepareReport(cri);
			model.addAttribute("leaves", l_LeaveList);
		} catch (SQLException e) {
			model.addAttribute("error", e.getMessage());
			return "error";
		}
		return "leave_rpt";
	}
}
